#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N=1e3+10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
char g[N][N];
int dist[N][N];
int n,m;
typedef pair<int, int> PII;
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
#define x first
#define y second

int bfs(PII start)
{
    queue<PII> q;
    memset(dist, -1, sizeof dist);

    int ans=0;
    q.push(start);


    while (q.size())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0; i < 4; i++)
        {
            int x = t.x + dx[i], y = t.y + dy[i];
            if (x < 1 || x > n || y < 1 || y > m) continue;  // 出界
            if (g[x][y] == '#') continue;  // 障碍物
            if (dist[x][y] != -1) continue;  // 之前已经遍历过

            dist[x][y] = dist[t.x][t.y] + 1;

          	if(g[x][y]=='!') ans++;

            q.push({ x, y });
        }
    }

    return ans;
}


int main()
{
	close();
	cin>>n>>m;
	PII start;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>g[i][j];
			if(g[i][j]=='@') start={i,j};
		}
	}	
	
	cout<<bfs(start)<<'\n';	
	
	return 0;
}